using System;
using System.Web.UI.WebControls;
using Academia.BI;
using Page=System.Web.UI.Page;

public partial class FamiliaBrowser : Page
{
    #region Load

    protected void Page_Load(object sender, EventArgs e)
    {
        //  ExecFilter();
    }

    #endregion

    protected void dsFamiliaEdit_Inserted(object sender, SqlDataSourceStatusEventArgs e)
    {
        int i = Convert.ToInt32(SqlDataManager.ExecuteScalar("SELECT MAX(FamiliaID) FROM Familia"));
        var cbPessoa = (DropDownList) dtlMain.FindControl("cbPessoa");

        SqlDataManager.ExecuteNonQuery(
            String.Format("Insert INTO PessoaFamilia (PessoaID, FamiliaID, Principal) VALUES ({0}, {1}, 1)",
                          cbPessoa.SelectedItem.Value, i));

        grdMain.DataBind();
    }

    protected void dsFamiliaEdit_Updated(object sender, SqlDataSourceStatusEventArgs e)
    {
        grdMain.DataBind();
    }

    protected void dsFamilia_Updated(object sender, SqlDataSourceStatusEventArgs e)
    {
        dtlMain.DataBind();
    }

    protected void dsFamilia_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        dtlMain.ChangeMode(DetailsViewMode.ReadOnly);
    }

    protected void dsPessoaFamilia_Inserting(object sender, SqlDataSourceCommandEventArgs e)
    {
        int i =
            Convert.ToInt32(
                SqlDataManager.ExecuteScalar("SELECT COUNT(PessoaID) FROM PessoaFamilia WHERE FamiliaID = " +
                                             grdMain.SelectedValue.ToString()));
        e.Command.Parameters[2].Value = (!(i > 0));
    }
}